from ..Chess import ChessBase
from ..Death import DeathBase
from ..Move import MoveBase
from ..Pos import Pos, PosSet
from ..Skill import SkillBase
from .Universal import *


class PawnMove(MoveBase):
    def __init__(self, chess: ChessBase):
        super().__init__(chess=chess)

    def get_movable(self):
        movable = PosSet()
        movable.add(d_forward(self.owner.pos, self.owner.player))
        movable.add(d_up(self.owner.pos, self.owner.player))
        movable.add(d_x_right_down(self.owner.pos, self.owner.player))
        return movable

    def get_reachable(self):
        tmp = self.get_movable()
        reachable = PosSet()
        for i in tmp:
            if self.owner.board.can_kill(self.owner, i):
                reachable.add(i)
        tmp = self.get_eatable()
        for i in tmp:
            if self.owner.board.has_chess(i) and self.owner.board.can_kill(self.owner, i):
                reachable.add(i)
        return reachable

    def can_move_to(self, player: int, x: Pos):
        if self.owner.player != player:
            return False
        if self.get_movable().exists(x):
            return self.owner.board.can_kill(self.owner, x)
        if self.get_eatable().exists(x):
            return self.owner.board.has_chess(x) and self.owner.board.can_kill(self.owner, x)
        return False

    def get_eatable(self):
        eatable = PosSet()
        eatable.add(d_down(self.owner.pos, self.owner.player))
        eatable.add(d_x_left_up(self.owner.pos, self.owner.player))
        return eatable

    def on_moved(self):
        return super().on_moved()


class PawnDeath(DeathBase):
    def __init__(self, chess):
        super().__init__(chess)

    def can_be_killed(self, killer):  # TODO 由于现在没有 stone 所以暂时不判断
        return True

    def on_death(self, killer):
        pass


class PawnSkill(SkillBase):
    def __init__(self, chess):
        super().__init__(chess)


class Pawn(ChessBase):
    def __init__(self, pos: Pos, player: int, board=None):
        super().__init__("Pawn", pos, player, PawnDeath(self),
                         PawnSkill(self), PawnMove(self), 1, 1, board)
